
#include "texture/texture.h"

namespace sheeplib {

	namespace texture {


		/// Create room for a blank texture using the format structure
		Texture::Texture(const TextureFormat& format) {
			unsigned int bytesPerPixel = format.m_BitsPerPixel / 8;
			m_Format.m_Width = format.m_Width;
			m_Format.m_Height = format.m_Height;
			m_Format.m_BitsPerPixel = format.m_BitsPerPixel;
			m_Format.m_ImageData.resize(format.m_Width * format.m_Height * bytesPerPixel);
		}

		/// Default constructor
		Texture::Texture() {
			m_Format.m_Width = 0;
			m_Format.m_Height = 0;
			m_Format.m_BitsPerPixel = 0;
		}


		Texture::~Texture() {
			FreeImageData();
		}

		void Texture::BuildTexture() {
			glGenTextures(1, &m_TextureID);
			glBindTexture(GL_TEXTURE_2D, m_TextureID);
			glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
			gluBuild2DMipmaps(GL_TEXTURE_2D,    GetFormat().m_BitsPerPixel / 8,
												GetFormat().m_Width,
												GetFormat().m_Height,
												GetType(), GL_UNSIGNED_BYTE, &GetFormat().m_ImageData[0]);
		}

		void Texture::FreeImageData() {
			m_Format.m_ImageData.clear();
		}

		string Texture::GetErrorString() {
			return m_ErrorString;
		}

		GLuint Texture::GetTexID() {
			return m_TextureID;
		}

	}
}
